package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Activity;
import com.example.demo.mapper.ActivityMapper;
import com.example.demo.mapper.ClassMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/activity")
public class ActivityController {

    @Resource
    ActivityMapper activityMapper;


    @PostMapping
    public Result<?> save(@RequestBody Activity activity) {

        activityMapper.insert(activity);
        return Result.success();

    }

    @PutMapping
    public Result<?> update(@RequestBody Activity activity) {
        activityMapper.updateById(activity);
        return Result.success();

    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        activityMapper.deleteById(id);
        return Result.success();

    }

    @GetMapping("/Query")
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {

        System.out.println("sss");

        LambdaQueryWrapper<Activity> wrapper = Wrappers.<Activity>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Activity::getActivityName, search);
        }
        Page<Activity> stuPage = activityMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(stuPage);

    }
}